﻿//fs
//#version 410 core

const int MAX_POINT_LIGHTS = 2;                                                             
const int MAX_SPOT_LIGHTS = 2;                                                              
                                                                                            
in vec2 TexCoord_FS_in;                                                                     
in vec3 Normal_FS_in;                                                                       
in vec3 WorldPos_FS_in;                                                                     
                                                                                            
out vec4 FragColor;                                                                         
                                                                                            
struct BaseLight                                                                            
{                                                                                           
    vec3 Color;                                                                             
    float AmbientIntensity;                                                                 
    float DiffuseIntensity;                                                                 
};                                                                                          
                                                                                            
struct DirectionalLight                                                                     
{                                                                                           
    //BaseLight Base;                                                                  
    vec3 Direction;                                                                         
};                                                                                          
                                                                                            
struct Attenuation                                                                          
{                                                                                           
    float Constant;                                                                         
    float Linear;                                                                           
    float Exp;                                                                              
};                                                                                          
                                                                                            
uniform DirectionalLight gDirectionalLight;                                                 
uniform sampler2D gColorMap;                                                                
uniform vec3 gEyeWorldPos;                                                                  
uniform float gMatSpecularIntensity;                                                        
uniform float gSpecularPower;                                                               
                                                                                            
vec4 CalcLightInternal(BaseLight Light, vec3 LightDirection, vec3 Normal)            
{                                                                                           
    vec4 AmbientColor = vec4(Light.Color * Light.AmbientIntensity, 1.0f);
    float DiffuseFactor = dot(Normal, -LightDirection);                                     
                                                                                            
    vec4 DiffuseColor  = vec4(0, 0, 0, 0);                                                  
    vec4 SpecularColor = vec4(0, 0, 0, 0);                                                  
                                                                                            
    if (DiffuseFactor > 0) {                                                                
        DiffuseColor = vec4(Light.Color * Light.DiffuseIntensity * DiffuseFactor, 1.0f);
                                                                                            
        vec3 VertexToEye = normalize(gEyeWorldPos - WorldPos_FS_in);                        
        vec3 LightReflect = normalize(reflect(LightDirection, Normal));                     
        float SpecularFactor = dot(VertexToEye, LightReflect);                              
        if (SpecularFactor > 0) {                                                           
            SpecularFactor = pow(SpecularFactor, gSpecularPower);                               
            SpecularColor = vec4(Light.Color * gMatSpecularIntensity * SpecularFactor, 1.0f);
        }                                                                                   
    }                                                                                       
                                                                                            
    return (AmbientColor + DiffuseColor + SpecularColor);                                   
}                                                                                           
                                                                                            
vec4 CalcDirectionalLight(vec3 Normal)                                                      
{                                                                                           
    return CalcLightInternal(gDirectionalLight.Base, gDirectionalLight.Direction, Normal);  
}                                                                                           
                                                                                            
void main()                                                                                 
{                                                                                           
    vec3 Normal = normalize(Normal_FS_in);                                                  
    vec4 TotalLight = CalcDirectionalLight(Normal);                                         
                                                                                            
    FragColor = texture(gColorMap, TexCoord_FS_in.xy) * TotalLight;
}